<template>
	<view>
		
		<input type="text" v-model="name" class="title" placeholder="请输入名称">
		<input type="text" v-model="content" class="title" placeholder="请输入介绍">
		<input type="text" v-model="rules" class="title" placeholder="请输入使用规则">
		<input type="number" v-model="money" class="title" placeholder="请输入购买金额">
		<input type="number" v-model="dis" class="title" placeholder="请输入折扣金额">
		
		<radio-group @change="changet" class="voucherType">
			<p style="display: block;margin: 0 auto;margin-bottom: 10px;">优惠券类型</p>
			
			<radio class="type" id="normal" value="0">
			<label for="normal" >普通</label></radio>
			
			<radio class="type" id="special" value="1">
			<label for="special" >特价</label></radio>
		</radio-group>
		
		<input v-if="type == 1" type="number" v-model="count" class="title" placeholder="请输入库存">
		
		<view style="clear: both;"></view>
		<picker v-if="type == 1" mode="date" :value="Startdate" start="1970-01-01" end="3000-01-01" @change="bindDateChange">
			<view class="labels">生效日期: </view>
			<view class="data">{{ Startdate }}</view>
		</picker>
		<picker v-if="type == 1" mode="time" :value="StartTimer" start="00:00" end="23:59" @change="bindTimeChange">
			<view class="data">{{ StartTimer }}</view>
		</picker>
		
		
		
		<picker v-if="type == 1" mode="date" :value="Enddate" start="1970-01-01" end="3000-01-01" @change="EndDateChange">
			<view class="labels">结束日期: </view>
			<view class="data">{{ Enddate }}</view>
		</picker>
		<picker v-if="type == 1" mode="time" :value="EndTimer" start="00:00" end="23:59" @change="EndTimeChange">
			<view class="data">{{ EndTimer }}</view>
		</picker>
		<view style="clear: both;height: 50px;"></view>
		<button @click="Add()">添加</button>
	</view>
</template>

<script>
	var type
	export default{
		name:'addvoucher',
		data(){
			return{
				type:type,
				Startdate:"2000-01-01",
				StartTimer: "00:00",
				Enddate:"2000-01-01",
				EndTimer:"00:00",
				name:"",
				content:"",
				rules:"",
				money:"",
				dis:"",
				count:""
			}
		},
		methods:{
			bindDateChange(e){
				this.Startdate = e.detail.value
			},
			bindTimeChange(e){
				this.StartTimer = e.detail.value
			},
			changet(e){
				this.type = e.detail.value
				type = e.detail.value
				console.log(this.type)
			},
			EndDateChange(e){
				this.Enddate = e.detail.value
			},
			EndTimeChange(e){
				this.EndTimer = e.detail.value
			},
			Add(){
				if(type == 1){
					uni.request({
						url: "http://" + getApp().globalData.IpAddr + "/voucher/seckill",
						method:"POST",
						header:{	'authorization':getApp().globalData.token	},
						data:{
							'goodsId' : getApp().globalData.goodsId,
							'title' : this.name,
							'subTitle' : this.content,
							'rules' : this.rules,
							'payValue' : this.money,
							'actualValue' : this.dis,
							'stock' : this.count,
							'beginTime' : this.Startdate + "T" + this.StartTimer,
							'endTime' : this.Enddate + "T" + this.EndTimer
						},
						success:res=> {
							// console.log(res)
							if(!res.data.success)	wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
							else{
								wx.showToast({	title: "添加成功",	duration: 2000	})
							}
						}
					})
				}else{
					uni.request({
						url: "http://" + getApp().globalData.IpAddr + "/voucher",
						method:"POST",
						header:{	'authorization':getApp().globalData.token	},
						data:{
							'goodsId' : getApp().globalData.goodsId,
							'title' : this.name,
							'subTitle' : this.content,
							'rules' : this.rules,
							'payValue' : this.money,
							'actualValue' : this.dis
						},
						success:res=> {
							// console.log(res)
							if(!res.data.success)	wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
							else{
								wx.showToast({	title: "添加成功",	duration: 2000	})
							}
						}
					})
				}
				
				
			}

		},
		onLoad:function(){
			this.type = 0
		}
	}
</script>

<style>
	.type{
		margin-left: 50px;
		margin-right: 50px;
	}
	.voucherType{
		display: block;
		text-align: center;
	}
	.labels{
		width: 20vw;
		padding: 10px;
		float: left;
	}
	.data{
		width: 25vw;
		/* background-color: red; */
		float: left;
		padding: 10px;
	}
	.title{
		margin: 5vw;
		width: 90vw;
		border-radius: 10px;
	}
</style>